{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "380aacb1-d7c0-4b1b-946e-ab284590b51e",
   "metadata": {},
   "source": [
    "Numpy的基本使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7172b55e-5fdd-4ad3-8c4e-ec50c32eee99",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    "# 1维\n",
    "a = np.array([1,2,3])  \n",
    "print (a)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "787cf0fc-ed44-4217-bdbb-c732ebb15553",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 1 2]\n",
      " [3 4 3 4]\n",
      " [5 6 5 6]]\n",
      "2\n",
      "(3, 4)\n"
     ]
    }
   ],
   "source": [
    "# 2维\n",
    "a = np.array([[1,  2, 1, 2],  [3,  4, 3, 4], [5, 6, 5, 6]])  \n",
    "print (a)\n",
    "print(a.ndim)\n",
    "print(a.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9739adf6-bc75-4c42-aa6d-0ce6fd9d20d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(b'abc', 21, 50.) (b'xyz', 18, 75.)]\n",
      "[21 18]\n",
      "[50. 75.]\n"
     ]
    }
   ],
   "source": [
    "# 可以为元素指定数据类型， S20, i1, f4 均是 numpy内置类型\n",
    "student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) \n",
    "a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student) \n",
    "print(a)\n",
    "print(a['age'])\n",
    "print(a['marks'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7f2b7e9c-a92a-42a7-aee5-c2ba5533a5d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.25\n",
      "1.118033988749895\n",
      "1.2500000000000002\n",
      "2.0\n",
      "3.0\n",
      "8\n",
      "10.0\n",
      "10.0\n"
     ]
    }
   ],
   "source": [
    "# 统计函数\n",
    "print (np.var([1,2,3,4])) # 方差\n",
    "print (np.std([1,2,3,4])) # 标准差\n",
    "print(np.std([1,2,3,4]) * np.std([1,2,3,4]))\n",
    "print (np.mean([1, 2, 3])) # 均值\n",
    "print (np.median([1, 2, 3, 3, 8, 9, 10])) # 中位数\n",
    "\n",
    "from scipy import stats\n",
    "nums = [0,4,5,5,8,8,8]\n",
    "print(stats.mode(nums)[0][0]) # 众数,数量最多的数\n",
    "\n",
    "a = np.array([10, 10])\n",
    "w = np.array([1./2, 1./2])\n",
    "w2 = np.array([1./3, 2./3])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "a7200535-64f0-454d-8a5a-1878cbad0bf3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.0\n"
     ]
    }
   ],
   "source": [
    "a = np.array([10, 10])\n",
    "weights = [1, 2]\n",
    "output = np.average(a, weights=weights)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fc8c483-dfd7-488d-8ad6-87ca197459d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(15).reshape(3,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df4484d8-2489-4f4f-9642-f647ffa3bb87",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1685483f-b402-484b-bd21-5291d201d825",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
